// Binary.cpp : implementation file
//

#include "stdafx.h"
#include "Binary.h"
#include "../../Maths/Mathematics.h"

// CBinary

CBinary::CBinary()
{
}

CBinary::~CBinary()
{
}


// CBinary member functions

BOOL CBinary::Apply(CxImage& srcImage, CxImage& destImage)
{
	int thresholdValue = CMathematics::OTSUThreshold(srcImage.GetAverageHistogram());

	if (srcImage.ResetIteration() && destImage.ResetIteration())
	{
		RGBQUAD value;
		UINT temp = 0;
		while (srcImage.GetNextPixelColor(value))
		{
			temp = 0.3 * (double)value.rgbRed + 
					0.59 * (double)value.rgbGreen + 
					0.11 * (double)value.rgbBlue;
			if (temp <= thresholdValue)
				value.rgbRed = value.rgbGreen = value.rgbBlue = 0;
			else
				value.rgbRed = value.rgbGreen = value.rgbBlue = 255;
			destImage.SetNextPixelColor(value);
		}
		destImage.SetBinarized(TRUE);
		return TRUE;
	}
	return FALSE;
}

BOOL CBinary::Apply(CxImage& srcImage)
{
	int thresholdValue = CMathematics::OTSUThreshold(srcImage.GetAverageHistogram());

	if (srcImage.ResetIteration())
	{
		RGBQUAD value;
		UINT temp = 0;
		while (srcImage.GetNextPixelColor(value))
		{
			temp = 0.3 * (double)value.rgbRed + 
					0.59 * (double)value.rgbGreen + 
					0.11 * (double)value.rgbBlue;
			if (temp <= thresholdValue)
				value.rgbRed = value.rgbGreen = value.rgbBlue = 0;
			else
				value.rgbRed = value.rgbGreen = value.rgbBlue = 255;
			srcImage.SetCurrentPixel(value);
		}
		srcImage.SetBinarized(TRUE);
		return TRUE;
	}
	return FALSE; 
}